﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class ReportDownload : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Joan 2008-10-16 disable autopostback
            if (this.DeleteButton.Attributes["autopostback"] == null)
                this.DeleteButton.Attributes.Add("autopostback", "false");

            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }

            else if (!Page.IsPostBack)
            {
                this.DeleteButton.Attributes.Add("onClick", "OnRptDelete();");
                this.GetData();
            }

        }

        protected void GetData()
        {
            ListItem[] list = new ListItem[8];

            list[0] = new ListItem("All", "All");
            list[1] = new ListItem("By CAT","ByCAT");
            list[2] = new ListItem("By PU", "ByBD");
            list[3] = new ListItem("By Province", "ByProv");
            list[4] = new ListItem("By Province By CAT", "ByProvByCAT");
            list[5] = new ListItem("By Province By PU", "ByProvByBD");
            list[6] = new ListItem("By CAT BY PU", "ByCATByBD");
            list[7] = new ListItem("By PU BY CAT", "ByBDByCAT");
            this.ConditionDropDownList.Items.Clear();

            for (int i = 0; i < list.Length; i++)
            {
                this.ConditionDropDownList.Items.Add(list[i]);
            }
   
            this.GetReport("ReportID <> 0 ORDER BY ReportType,ReportDate Desc");
        }

        protected void GetReport(string condition)
        {
            try
            {
                BEGDC.PreContract.BLL.IReportHistory iReport = (BEGDC.PreContract.BLL.IReportHistory)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ReportHistory_Service];
                DataTable dt = iReport.GetReportHistory_DataTable(condition);

                this.gvReport.DataSource = dt;
                this.gvReport.DataBind();
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", "<script language='javascript'>alert('" + ex.Message.Replace("'", "\\'") + "')</script>", false);
            }
        }

        

        protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                ImageButton imgedit = (ImageButton)e.Row.FindControl("imgedit");
                imgedit.Attributes.Add("onclick", "javascript:DownLoadRpt('" + this.gvReport.DataKeys[e.Row.RowIndex].Value.ToString() + "');return false;");
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this.SearchReport();
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                BEGDC.PreContract.BLL.IReportHistory iReport = (BEGDC.PreContract.BLL.IReportHistory)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ReportHistory_Service];
                CheckBox chkSelect = new CheckBox();
                int num = 0;

                for (int i = 0; i < this.gvReport.Rows.Count; i++)
                {
                    chkSelect = (CheckBox)this.gvReport.Rows[i].FindControl("chkSelect");

                    if (chkSelect.Checked)
                    {
                        iReport.DeleteReportHistory(Convert.ToInt32(this.gvReport.DataKeys[i].Value.ToString()));
                        num++;
                    }
                }

                if (num == 0)
                {
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", "<script language='javascript'>alert('请选择至少一项!\\n Please select at least one item!')</script>", false);
                    return;
                }

                ScriptManager.RegisterStartupScript(this.UpdatePanel1,this.GetType(),"","<script language='javascript'>alert('成功删除"+num+"条数据!\\n Deleted "+num+" items successfully!')</script>",false);

                this.SearchReport();
                
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", "<script language='javascript'>alert('" + ex.Message.Replace("'", "\\'") + "')</script>",false);
            }
        }

        protected void SearchReport()
        {
            string condition = string.Empty;

            if (this.ConditionDropDownList.SelectedIndex == 0)
                condition = "ReportID <> 0 ";

            else
            {
                condition = this.ConditionDropDownList.SelectedValue;
                condition = String.Format("ReportType = '{0}'", condition);
            }

            if (this.TimeTextBox.Text.Trim().Equals(string.Empty))
                condition += "ORDER BY ReportType, ReportDate Desc";
            else
                condition += String.Format(" AND ReportDate = '{0}' ORDER BY ReportType, ReportDate Desc", this.TimeTextBox.Text.Trim());

            this.GetReport(condition);
        }
    }
}
